package fr.umlv.galcon.draw;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import zen2.ApplicationRenderCode;
import fr.umlv.galcon.game.GalconManager;
import fr.umlv.galcon.item.ItemListSingleton;
import fr.umlv.galcon.player.Player;
import fr.umlv.galcon.player.PlayerType;
import fr.umlv.galcon.ship.ShipAbstract;
import fr.umlv.galcon.utils.Utils;

public class GalconDraw implements ApplicationRenderCode {

    /**
     * Function which draw the world
     */
    @Override
    public void render(Graphics2D graphics) {

	if (!GalconManager.isOver() && !GalconManager.getEndGame()) {
	    graphics.setBackground(Color.WHITE);
	    graphics.clearRect(0, 0, Utils.FRAME_WIDTH, Utils.FRAME_HEIGHT - Utils.INFO_AREA);

	    graphics.setBackground(Color.BLACK);
	    graphics.clearRect(0, Utils.FRAME_HEIGHT - Utils.INFO_AREA, Utils.FRAME_WIDTH, Utils.FRAME_HEIGHT);

	    for (int i = 0; i < ItemListSingleton.getInstance().getPlanetList().size(); i++) {
		ItemListSingleton.getInstance().getPlanetList().get(i).draw(graphics);
	    }

	    int size = ItemListSingleton.getInstance().getShipList().size();
	    for (int i = 0; i < size; i++) {
		ShipAbstract ship = ItemListSingleton.getInstance().getShipList().get(i);
		if (ship != null) {
		    if (ship.isRemoved()) {
			synchronized (ItemListSingleton.getInstance().getShipList()) {
			    ship.getSquadron().getSquadronList().remove(ship);
			    ItemListSingleton.getInstance().getShipList().remove(ship);
			    size--;
			}

		    } else {
			ship.draw(graphics);
		    }
		}

	    }
	    drawInfoArea(graphics);
	} else {
	    graphics.setColor(Color.WHITE);
	    graphics.clearRect(0, 0, Utils.FRAME_WIDTH, Utils.FRAME_HEIGHT);
	    try {
		graphics.drawImage(Utils.getWinnerFile(), null, 0, 0);
	    } catch (IOException e) {
		graphics.clearRect(0, 0, Utils.FRAME_WIDTH, Utils.FRAME_HEIGHT);

	    }
	    graphics.setFont(new Font("Arial", Font.BOLD, 30));
	    graphics.drawString(GalconManager.getWinner().getName(), Utils.FRAME_WIDTH - 800, Utils.FRAME_HEIGHT - 100);
	}
    }

    /**
     * Function wich draw the information area.
     * 
     * It display the player's score, and the information of the game play.
     * 
     * @param graphics
     */
    private void drawInfoArea(Graphics2D graphics) {
	int x = 15;
	graphics.setColor(Color.WHITE);

	graphics.drawString(GalconManager.getScroll() + "%", Utils.FRAME_WIDTH - 50, Utils.FRAME_HEIGHT - 50);

	graphics.drawString("[A] : Select all", 10, Utils.FRAME_HEIGHT - Utils.INFO_AREA + x);
	x += 30;
	graphics.drawString("[S] Strong ship", 10, Utils.FRAME_HEIGHT - Utils.INFO_AREA + x);
	x += 15;
	graphics.drawString("[M] Medium ship", 10, Utils.FRAME_HEIGHT - Utils.INFO_AREA + x);
	x += 15;
	graphics.drawString("[W] Week ship", 10, Utils.FRAME_HEIGHT - Utils.INFO_AREA + x);

	Map<Player, Integer> map = GalconManager.getPlayers();
	Player player;
	Set<Player> entries = map.keySet();
	Iterator<Player> it = entries.iterator();

	int y = Utils.FRAME_HEIGHT - Utils.INFO_AREA + 15;
	while (it.hasNext()) {
	    player = it.next();
	    if (player.getPlayerType() != PlayerType.NEUTRAL) {
		graphics.drawString(player.getName() + " : " + map.get(player), Utils.FRAME_WIDTH - 100, y);
		y = y + 15;
	    }
	}
    }
}
